﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CWCS.Common;
using System.IO;
using System.Threading;
using ONNONLed5KSDKD;

namespace CWCS.BLL
{

    public class LED_UDP : BLLBase
    {
        public class AddClass
        {
            public static string Add(string a, string b)
            {
                Console.WriteLine("\n开始计算：" + a + "+" + b );
                Thread.Sleep(3000); //模拟该方法运行三秒
                Console.WriteLine("计算完成！");
                return a  ;
            }
        }
        public int CountSend1 = 0;//LED1发送次数限制
        public int CountSend2 = 0;//LED2发送次数限制
        public int CountSend3 = 0;//LED3发送次数限制
        public int CountSend4 = 0;//LED4发送次数限制
        public bool SendEnable = false;//发送使能
        //public uint i;
        public static byte[] AreaText;
        public static byte[] SoundAreaText;
        public byte SoundMode;
        public byte SoundPerson;
        public byte SoundVolume;
        public byte SoundSpeed;
        public int SoundDataLen;
        public string YB_Port; //端口
        public string YB_IP;  //IP
        public string YB_Type;//控制器类型
        public bool YB_Flag;//控制器连接标识
        public string XPos = "0";//X坐标
        public string YPos = "0";//Y坐标
        public string Height = "192";//区域高度
        public string Width = "64";//区域宽度
        public int ShowLine = 1;//显示行数 0:单行显示  1:多行显示
        public int ChangeLine = 1;//换行方式 0:手动换行 1:自动换行
        public int LineSpacing = 0;//行间距(0-100点)
        public int RunningMode = 0;//运行模式 0:自动循环显示 1:完成后停留在最后一页 2:超时未完成删除该信息
        public int Limit = 2;//超时时间(秒)
        public int ShowStunt = 1;//显示特技 0:静止显示 1:快速打出 2:向左移动 3:向右移动 4:向上移动 5:向下移动
        public int RunningSpeed = 1;//运行速度
        public int StayTime = 10;//停留时间，单位(个0.5秒)
        public int Voice = 0;//不允许播放语音
        public int VoiceMan = 0;//播音人
        public int VoiceNum = 0;//音量
        public int VoiceVelocity = 0;//语速
        public string VoiceMsg = "";//语音内容
        public Dictionary<string, LED_UDP> DLed = new Dictionary<string, LED_UDP>();
        public Led5kSDK.bx_5k_area_header bx_5k;
        private static Dictionary<string, string> LED_UDP_LOG = new Dictionary<string, string>();
        public delegate void LEDUpdateTxt(string msg);
        public delegate string AddHandler(string a, string b);
        //定义一个委托变量
         public  AddHandler handler;
        public LEDUpdateTxt LEDupdateTxt;
        public uint m_dwCurHand;

        public LED_UDP()
        {
           ONNONLed5KSDKD.Led5kSDK.InitSdk(2, 2);//初始化 20200311屏蔽
        }

        public LED_UDP(string equipmet, string IP, string Port, string Type)
        {
            YB_IP = IP;
            YB_Port = Port;
            YB_Type = Type;
            YB_Flag = false;
            m_dwCurHand = 0;
            bx_5k = new Led5kSDK.bx_5k_area_header();
        }

        //创建固定IP通讯模式
        private bool ybConnect(string equipment)
        {
            if (!DLed[equipment].YB_Flag)
            {
                byte[] led_ip = System.Text.Encoding.ASCII.GetBytes(DLed[equipment].YB_IP);
                uint led_port = Convert.ToUInt32(DLed[equipment].YB_Port);
                byte[] card_type_list = new byte[12];
                card_type_list[0] = 0xFE;//任意
                card_type_list[1] = 0x51;//BX_5K1
                card_type_list[2] = 0x58;//BX_5K2
                card_type_list[3] = 0x54;//BX_5MK1
                card_type_list[4] = 0x53;//BX_5MK2
                card_type_list[5] = 0x5c;//BX_5K1Q_YY
                card_type_list[6] = 0x61;//BX_6K1
                card_type_list[7] = 0x62;//BX_6K2
                card_type_list[8] = 0x63;//BX_6K3
                card_type_list[9] = 0x64;//BX_6K1_YY
                card_type_list[10] = 0x65;//BX_6K2_YY
                card_type_list[11] = 0x66;//BX_6K3_YY
                int t = getType(DLed[equipment].YB_Type);
                uint hand = ONNONLed5KSDKD.Led5kSDK.CreateClient(led_ip, led_port, (Led5kSDK.bx_5k_card_type)card_type_list[t], 1, 1, null);
                DLed[equipment].m_dwCurHand = hand;
                if (hand == 0)
                {
                    DLed[equipment].YB_Flag = false;
                    return false;
                }
                else
                {
                    DLed[equipment].YB_Flag = true;
                    return true;
                }
            }
            else
            {
                DLed[equipment].YB_Flag = true;
                return true;
            }
        }

        //断开连接(应该是全部连接都断开)
        private void CloseDestroy(uint m_dwCurHand)
        {
           // ONNONLed5KSDKD.Led5kSDK.Destroy(m_dwCurHand);20200311屏蔽
        }

        //控制器型号转换
        public int getType(string model)
        {
            int type;
            switch (model)
            {
                case "BX_5K1":
                    type = 1;
                    break;
                case "BX_5K2":
                    type = 2;
                    break;
                case "BX_5MK1":
                    type = 3;
                    break;
                case "BX_5MK2":
                    type = 4;
                    break;
                case "BX_5K1Q_YY":
                    type = 5;
                    break;
                case "BX_6K1":
                    type = 6;
                    break;
                case "BX_6K2":
                    type = 7;
                    break;
                case "BX_6K3":
                    type = 8;
                    break;
                case "BX_6K1_YY":
                    type = 9;
                    break;
                case "BX_6K2_YY":
                    type = 10;
                    break;
                case "BX_6K3_YY":
                    type = 11;
                    break;
                default:
                    type = 0;
                    break;
            }
            return type;
        }
        
        //发送动态区域更新函数
        public bool send_Msg(string equipmentnum,string msgType, string msg)
        {

            if (LED_UDP_LOG.ContainsKey(equipmentnum))
            {
                if (LED_UDP_LOG[equipmentnum].Contains(msg))
                {
                    return false;
                }
                else
                {
                    LED_UDP_LOG[equipmentnum] = msg;
                }
            }
            else
            {
                LED_UDP_LOG.Add(equipmentnum, msg);
            }

            if (ybConnect(equipmentnum))
            {
                DLed[equipmentnum].bx_5k.DynamicAreaLoc = (byte)0;
                AreaText = System.Text.Encoding.Default.GetBytes(msg.Trim());
                DLed[equipmentnum].bx_5k.DataLen = AreaText.Length;
                int x;
                x = ONNONLed5KSDKD.Led5kSDK.SCREEN_SendDynamicArea(DLed[equipmentnum].m_dwCurHand, DLed[equipmentnum].bx_5k, (ushort)DLed[equipmentnum].bx_5k.DataLen, AreaText);
                if (x == 0)
                {
                    return true;
                }
                else
                {
                    //连接失败，重新连接
                    DLed[equipmentnum].YB_Flag = false;
                    ybConnect(equipmentnum);
                    return false;
                }
            }
            else
                return false;
        }
        public void UpdateTxtMethod(string msg)
        {
         
        }
        public void ThreadMethodTxt(string equipmentnum)
        {
            // LEDupdateTxt.BeginInvoke(UpdateTxtMethod, equipmentnum);
            try
            {
                string LEDmsg = LED_UDP_LOG[equipmentnum];
                DLed[equipmentnum].bx_5k.DynamicAreaLoc = (byte)0;
                AreaText = System.Text.Encoding.Default.GetBytes(LEDmsg.Trim());
                DLed[equipmentnum].bx_5k.DataLen = AreaText.Length;
                int x;
                x = ONNONLed5KSDKD.Led5kSDK.SCREEN_SendDynamicArea(DLed[equipmentnum].m_dwCurHand, DLed[equipmentnum].bx_5k, (ushort)DLed[equipmentnum].bx_5k.DataLen, AreaText);
            }
            catch
            { }
        }
        //初始化LED
        public void init(string equipment)
        {
            DLed[equipment].bx_5k.AreaType = 0x06;
            DLed[equipment].bx_5k.AreaX = Convert.ToInt16(XPos);
            DLed[equipment].bx_5k.AreaX /= 8;
            DLed[equipment].bx_5k.AreaY = Convert.ToInt16(YPos);
            DLed[equipment].bx_5k.AreaWidth = Convert.ToInt16(Height);
            DLed[equipment].bx_5k.AreaWidth /= 8;
            DLed[equipment].bx_5k.AreaHeight = Convert.ToInt16(Width);
            DLed[equipment].bx_5k.Lines_sizes = Convert.ToByte(LineSpacing.ToString());

            byte[] RunMode_list = new byte[3];
            RunMode_list[0] = 0;
            RunMode_list[1] = 1;
            RunMode_list[2] = 2;
            int rl = RunningMode;
            DLed[equipment].bx_5k.RunMode = RunMode_list[rl];
            //bx_5k.RunMode = Convert.ToByte(comboBox3.SelectedIndex+1);

            DLed[equipment].bx_5k.Timeout = Convert.ToInt16(Limit.ToString());

            byte[] SoundMode_list = new byte[3];
            SoundMode_list[0] = 0;
            SoundMode_list[1] = 1;
            SoundMode_list[2] = 2;
            int sml = Voice;
            SoundMode = SoundMode_list[sml];

            byte[] SoundPerson_list = new byte[6];
            SoundPerson_list[0] = 0;
            SoundPerson_list[1] = 1;
            SoundPerson_list[2] = 2;
            SoundPerson_list[3] = 3;
            SoundPerson_list[4] = 4;
            SoundPerson_list[5] = 5;
            int spl = VoiceMan;
            SoundPerson = SoundPerson_list[spl];

            SoundVolume = (byte)VoiceNum;

            SoundSpeed = (byte)VoiceVelocity;

            SoundAreaText = System.Text.Encoding.Default.GetBytes(VoiceMsg);
            SoundDataLen = SoundAreaText.Length;


            DLed[equipment].bx_5k.Reserved1 = 0;
            DLed[equipment].bx_5k.Reserved2 = 0;
            DLed[equipment].bx_5k.Reserved3 = 0;

            byte[] SingleLine_list = new byte[2];
            SingleLine_list[0] = 0x01;
            SingleLine_list[1] = 0x02;
            int sll = ShowLine;
            DLed[equipment].bx_5k.SingleLine = SingleLine_list[sll];
            //bx_5k.SingleLine = Convert.ToByte(comboBox1.SelectedIndex);

            byte[] NewLine_list = new byte[2];
            NewLine_list[0] = 0x01;
            NewLine_list[1] = 0x02;
            int nl = ChangeLine;
            DLed[equipment].bx_5k.NewLine = NewLine_list[nl];
            //bx_5k.NewLine = Convert.ToByte(comboBox2.SelectedIndex);


            byte[] DisplayMode_list = new byte[6];
            DisplayMode_list[0] = 0x01;
            DisplayMode_list[1] = 0x02;
            DisplayMode_list[2] = 0x03;
            DisplayMode_list[3] = 0x04;
            DisplayMode_list[4] = 0x05;
            DisplayMode_list[5] = 0x06;
            int dml = ShowStunt;
            DLed[equipment].bx_5k.DisplayMode = DisplayMode_list[dml];
            //bx_5k.DisplayMode = Convert.ToByte(comboBox4.SelectedIndex);

            DLed[equipment].bx_5k.ExitMode = 0x00;


            DLed[equipment].bx_5k.Speed = (byte)RunningSpeed;
            //bx_5k.Speed=Convert.ToByte(comboBox5.SelectedIndex);

            DLed[equipment].bx_5k.StayTime = Convert.ToByte(StayTime);
        }
    }
}
